/****************************************************/
// Theme name: Nexus - Bootstrap Admin Template
// Theme URI: http://nexus.lethemes.net/1.0/html/
// Description: Nexus is a fully responsive admin dashboard template
// Tags: admin, admin dashboard, cms, crm, project managerment, responsive, webapp, backend, bootstrap admin, responsive admin
// Author: Loi Le
// Version: 1.0
/****************************************************/

'use strict';

$(document).ready(function() {

    // Fixed Sidebar
    // --------------------------------------------------
    var sidebarHeight = function() {
        if (!$('.main-sidebar').hasClass('fixed')) {
            if ($('.main-sidebar').height() <= $('.content').height()) {
                if ($('body').hasClass('header-fixed')) {
                    $('.main-sidebar').css('min-height', $('.content').height() - parseInt($('.main-sidebar').css('top')));
                } else {
                    $('.main-sidebar').css('min-height', $('.content').height() + parseInt($('header').css('height')));
                }
            }
        }
    }

    // Element Blocking
    // --------------------------------------------------
    function blockUI(element) {
        $(element).block({
            message: '<img src="../assets/images/reload.gif" width="30" alt="">',
            css: {
                border: 'none',
                padding: '0px',
                width: '30px',
                height: '30px',
                backgroundColor: 'transparent'
            },
            overlayCSS: {
                backgroundColor: '#3B40CE',
                opacity: 0.1,
                cursor: 'wait'
            }
        });
    }

    function unblockUI(element) {
        $(element).unblock();
    }

    // Avoid General Settings Dropdown Menu Close On Click Inside
    // --------------------------------------------------
    $('.dropdown-menu').filter('.setting').click(function(e) {
        e.stopPropagation();
    });

    // Pin Sidebar
    // --------------------------------------------------
    $('#pin-sidebar').on('click', function() {
        if ($(this).prop('checked') == true) {
            $('#closed-sidebar-check').prop('checked', false);
            $('#pinned-sidebar-check').prop('checked', true);
            $('.main-sidebar').addClass('pinned').removeClass('collapsed');
        } else if ($(this).prop("checked") == false) {
            $('#closed-sidebar-check').prop('checked', true);
            $('#pinned-sidebar-check').prop('checked', false);
            $('.main-sidebar').removeClass('pinned').addClass('collapsed');
        }
    });

    // Toggle Sidebar
    // --------------------------------------------------
    $('.toggle-sidebar').on('click', function() {
        $('.main-sidebar').toggleClass('collapsed').removeClass('pinned');
        $('#pin-sidebar').prop('checked', false);
        $('#pinned-sidebar-check').prop('checked', false);
        if ($('.main-sidebar').hasClass('collapsed')) {
            $('#closed-sidebar-check').prop('checked', true);
        } else {
            $('#closed-sidebar-check').prop('checked', false);
        }
    });

    // Toogle Searchbar
    // --------------------------------------------------
    $('.show-search-bar, .close-search-bar').on('click', function() {
        $('.search-bar').toggleClass('collapsed');
    });

    // Toggle Right Sidebar
    // --------------------------------------------------
    $('.toggle-right-sidebar').on('click', function() {
        $('.right-sidebar').toggleClass('collapsed');
    });

    // Detect Medium Device
    // --------------------------------------------------
    if ($(window).width() < 1200) {
        $('.main-sidebar').removeClass('pinned').addClass('collapsed');
    };

    if ($(window).width() < 768) {
        $('.main-sidebar').removeClass('fixed');
        $('.main-sidebar').mCustomScrollbar('destroy');
        sidebarHeight();
    };

    // Tooltips & Popovers
    // --------------------------------------------------
    $('[data-toggle="tooltip"]').tooltip();
    $('[data-toggle="popover"]').popover();

    // Waves Effect
    // --------------------------------------------------
    Waves.attach('.btn-rounded', ['waves-button']);
    Waves.attach('.btn-circle', ['waves-circle', 'waves-float']);
    Waves.attach('.btn');
    Waves.init();

    // Layouts
    // --------------------------------------------------
    $('.navigation').on('hidden.bs.collapse', function() {
        sidebarHeight();
    });

    $('#fixed-header-check').on('click', function() {
        $('body').toggleClass('header-fixed')
        sidebarHeight();
    });

    $('#fixed-sidebar-check').on('click', function() {
        if ($(this).prop('checked') == true) {
            $('.main-sidebar').addClass('fixed').removeAttr('style');
            $('.main-sidebar').mCustomScrollbar({
                theme: $('.main-sidebar').data('theme') || 'dark',
                autoHideScrollbar: $('.main-sidebar').data('autoHideScrollbar') || false,
                mouseWheel: {
                    scrollAmount: $('.main-sidebar').data('scrollAmount') || 'auto',
                },
            });
        } else if ($(this).prop("checked") == false) {
            $('.main-sidebar').removeClass('fixed');
            $('.main-sidebar').mCustomScrollbar('destroy');
        }
        sidebarHeight();
    });

    $('#pinned-sidebar-check').on('click', function() {
        if ($(this).prop('checked') == true) {
            $('#closed-sidebar-check').prop('checked', false);
            $('#pin-sidebar').prop('checked', true);
            $('.main-sidebar').addClass('pinned').removeClass('collapsed');
        } else if ($(this).prop("checked") == false) {
            $('#closed-sidebar-check').prop('checked', true);
            $('#pin-sidebar').prop('checked', false);
            $('.main-sidebar').removeClass('pinned').addClass('collapsed');
        }
    });

    $('#closed-sidebar-check').on('click', function() {
        $('.main-sidebar').toggleClass('collapsed').removeClass('pinned');
        $('#pin-sidebar').prop('checked', false);
        $('#pinned-sidebar-check').prop('checked', false);
    });

    // Widget Control
    // --------------------------------------------------
    $('.widget-collapse').on('click', function() {
        $(this).closest('.widget').find('.widget-body').slideToggle(300);
    });
    $('.widget-reload').on('click', function() {
        var element = $(this).closest('.widget').find('.widget-body');
        blockUI(element);
        window.setTimeout(function() {
            unblockUI(element);
        }, 2000);
    });
    $('.widget-remove').on('click', function() {
        $(this).closest('.widget').hide();
    });

    // Themes
    // --------------------------------------------------
    $('.color-sidebar').on('click', function () {
        var setColor = $(this).attr('data-color');
        var getColor = $('body').attr('data-color');
        $('body').removeClass(getColor).addClass(setColor).attr('data-color', setColor);
    });
})